package com.ithuangyl.common.base;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;
import com.ithuangyl.common.R;
import com.ithuangyl.common.widget.topbar.TopBar;

import java.util.ArrayList;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/7/7 22:25
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    protected Activity mActivity;
    protected TopBar vTopBar;
    private FrameLayout vContentLayout;
    protected ImmersionBar mImmersionBar;
    private View statusBarView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView(R.layout.activity_base_top_bar);
        vTopBar = $(R.id.id_top_bar);
        //初始化设置 Toolbar
        if (null != vTopBar) {
            setSupportActionBar(vTopBar);
            if (getSupportActionBar() != null) {
                getSupportActionBar().setDisplayShowTitleEnabled(false);
            }
        }
        initImmersionBar();
        vContentLayout = $(R.id.id_content_layout);
        if (getLayout() != 0) {
            LayoutInflater.from(this).inflate(getLayout(), vContentLayout);
        }
        initView();
        initViewed();
        initData();
    }

    /**
     * 获取Layout布局
     *
     * @return
     */
    protected abstract int getLayout();

    /**
     * 初始化布局资源
     */
    protected abstract void initView();

    protected void initViewed() {

    }

    protected void initData() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 模仿jq将找View的方法命名为$
     *
     * @param id
     * @param <T>:泛型的写法省去强转代码
     * @return
     */
    public <T extends View> T $(@IdRes int id) {
        return findViewById(id);
    }

    protected void addClickListener(View view) {
        if (view != null) {
            view.setOnClickListener(this);
        }
    }

    protected void addClickListener(@IdRes int id) {
        addClickListener($(id));
    }

    @Override
    public void onClick(View view) {
        onViewClicked(view.getId());
    }

    public void onViewClicked(@IdRes int id) {

    }

    public void addIdsClickListener(ArrayList<Integer> ids) {
        if (ids != null) {
            for (Integer id : ids) {
                addClickListener(id);
            }
        }
    }

    public void addViewsClickListener(ArrayList<View> views) {
        if (views != null) {
            for (View view : views) {
                addClickListener(view);
            }
        }
    }

    /**
     * 初始化沉浸式状态栏
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f);
        mImmersionBar.init();
    }

    public void statusBarDarkFont(boolean isDarkFont, float statesAlpha) {
        if (mImmersionBar != null) {
            mImmersionBar.statusBarDarkFont(isDarkFont, statesAlpha).init();
        }
    }

    /**
     * 设置状态栏drawableId（可以实现渐变）
     */
    public void setStatusBarView(int drawableId) {
        //延时加载数据，保证statusBar绘制完成后再findView。
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                initStatusBar(drawableId);
                //不加监听,也能实现改变statusBar颜色的效果。但是会出现问题：比如弹软键盘后,弹PopWindow后,引起window状态改变时,statusBar的颜色就会复原.
                getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                    @Override
                    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                        initStatusBar(drawableId);
                        getWindow().getDecorView().removeOnLayoutChangeListener(this);
                    }
                });
                //只走一次
                return false;
            }
        });
    }

    /**
     * 颜色渐变
     */
    private void initStatusBar(int drawableId) {
        if (statusBarView == null) {
            //利用反射机制修改状态栏背景
            int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
            statusBarView = getWindow().findViewById(identifier);
        }
        if (statusBarView != null) {
            statusBarView.setBackgroundResource(drawableId);
        } else {
            Log.e("TeamManagementActivity", "statusBarView is null.");
        }
    }
}
